package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.shape.layout.ShapeRecyclerView;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.uiutil.PanelBindConfigUIUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class PanelBindStep1 extends AppActivity {
    private final String SCENE = "SCENE";
    private ShapeRecyclerView mList;
    private HashMap<String, PanelBindConfig> mConfigData;
    private int mBindUIType;
    private String mPanelSalveSn;
    private int mTypeCount;
    private LinkedHashMap<Integer, PanelBindConfig> mAllConfigItems = new LinkedHashMap<>();
    @DrawableRes int mUIIconResID;
    private String mUITypeName;
    private ConfigAdapter configAdapter;

    @Log
    public static void start(Context context, String panelSalveSn,String productModel, int bindUIType, @DrawableRes int uiIconRes, String uiName, int typeCount, HashMap<String, PanelBindConfig> configData) {
        Intent intent = new Intent(context, PanelBindStep1.class);
        intent.putExtra("bindUIType", bindUIType);
        intent.putExtra("configData", configData);
        intent.putExtra("panelSalveSn", panelSalveSn);
        intent.putExtra("typeCount", typeCount);
        intent.putExtra("uiIconRes", uiIconRes);
        intent.putExtra("uiName", uiName);
        intent.putExtra("productModel", productModel);

        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_panel_bind_step1;
    }

    @Override
    protected void initView() {
        String productModel = getString("productModel");
        if(productModel.equals("0x1005")){
            getTitleBar().getRightView().setVisibility(View.VISIBLE);
        }else{
            getTitleBar().getRightView().setVisibility(View.GONE);
        }
        mList = findViewById(R.id.list);
        mList.setLayoutManager(new LinearLayoutManager(getContext()));
        configAdapter = new ConfigAdapter();
        mList.setAdapter(configAdapter);
        configAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (configAdapter.getData().get(position).getBindSateType() == 2){
                    //TODO
                    PanelBindInfoActivity.start(getContext(), configAdapter.getData().get(position));
                }else {
                    //TODO
                    PanelBindSelectActivity.start(getContext(), configAdapter.getData().get(position));
                }
            }
        });
    }

    @Override
    protected void initData() {
        mBindUIType = getInt("bindUIType");
        mPanelSalveSn = getString("panelSalveSn");
        mTypeCount = getInt("typeCount");
        mUIIconResID = getInt("uiIconRes");
        mUITypeName = getString("uiName");
        mConfigData = (HashMap<String, PanelBindConfig>) getIntent().getSerializableExtra("configData");
        setTitle(String.format(getString(R.string.type_class), mUITypeName));
        createBindUI();
        configAdapter.setNewInstance(new ArrayList<>(mAllConfigItems.values()));
    }

    @Override
    public void onRightClick(View view) {
        PanelBindConfig config = new PanelBindConfig();
        config.setBindUIType(mBindUIType);
        config.setHouseSerialNo(AppApplication.getInstance().getCurrentHouseSerialNo());
        config.setPanelIndex(0);
        config.setPanelSalveSn(mPanelSalveSn);
        PanelBindSelectActivity.start(getContext(), config);
    }

    private void createBindUI() {
        for (int i = 1; i <= mTypeCount; i++) {
            PanelBindConfig config = new PanelBindConfig();
            config.setBindUIType(mBindUIType);
            config.setHouseSerialNo(AppApplication.getInstance().getCurrentHouseSerialNo());
            config.setPanelIndex(i);
            config.setPanelSalveSn(mPanelSalveSn);
            mAllConfigItems.put(i, config);
        }
        for (PanelBindConfig config : mConfigData.values()){
            mAllConfigItems.put(config.getPanelIndex(), config);
        }

    }

    private class ConfigAdapter extends BaseQuickAdapter<PanelBindConfig, BaseViewHolder>{

        public ConfigAdapter() {
            super(R.layout.panel_config_item);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, PanelBindConfig item) {
            holder.setImageResource(R.id.device_icon, mUIIconResID);
            if (item.getBindSateType() == 2){
                holder.setGone(R.id.bind_ly, false);
                holder.setGone(R.id.unbind_text, true);
                PanelBindConfigUIUtil panelBindConfigUIUtil = new PanelBindConfigUIUtil(getContext(), item);
                holder.setText(R.id.device_name, panelBindConfigUIUtil.getConfigItemName());
                holder.setText(R.id.bind_type, panelBindConfigUIUtil.getBindTypeStr());
            }else {
                holder.setGone(R.id.bind_ly, true);
                holder.setGone(R.id.unbind_text, false);
                holder.setText(R.id.unbind_text, String.format(getString(R.string.todo_bind_device), mUITypeName));
            }
        }
    }
}